package main

import (
	"AdvanceGo/12_grpc/service"
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"time"
)

func main() {
	// 创建连接
	conn, err := grpc.Dial("localhost:8999",
		grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			panic(err)
		}
	}(conn)

	// 新建客户端
	c := service.NewProductServiceClient(conn)
	// 一秒超时
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	// 获取数据
	resp, err := c.GetProductStock(ctx, &service.ProductRequest{ProdId: 1})
	if err != nil {
		log.Fatalln(err)
	}
	log.Println("responseMsg:", resp.String())
}
